<template>
  <div class="image-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>管理素材</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="action-head">
        <el-radio-group v-model="collect" size="mini" @change="onCollectChange">
          <el-radio-button :label="false">全部</el-radio-button>
          <el-radio-button :label="true">收藏</el-radio-button>
        </el-radio-group>
        <!-- 添加素材按钮 -->
        <el-button size="mini" type="primary" @click="dialogVisible = true"> 添加素材 </el-button>
      </div>
      <!-- 素材列表 -->
        <el-row :gutter="10">
          <el-col :xs="12" :sm="6" :md="6" :lg="3" v-for="(img, index) in images"
              :key="index" style="">
            <div class="Img">
              <el-image
              style="height: 150px"
              :src="img.url"
              fit="cover">
            </el-image>
            <div class="del">
              <!-- <i class="el-icon-star-off ico1"></i> -->
              <i :class="{'el-icon-star-off': true, ico1: img.is_collected }" @click="onSetImg(img.id, img)"></i>
              <i class="el-icon-delete ico" @click="ondelImg(img.id)"></i>
            </div>
            </div>
          </el-col>
        </el-row>
        <!-- 素材列表 -->
        <!-- 分页 -->
        <el-pagination
        layout="prev, pager, next"
        background
        :total="totalCount"
        :page-size="pageSize"
        @current-change="onCurrentChange"
        :disabled="loading"
        :current-page.sync="page">
        </el-pagination>
        <!-- 分页end -->
    </el-card>
    <el-dialog
    title="上传素材"
    :visible.sync="dialogVisible"
    width="60%"
    :before-close="handleClose">
      <el-upload
      class="upload-demo"
      drag
      action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
      multiple
      :headers="uploadHeaders"
      name="image"
      :on-success="onUploadSuccess">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getImages, setImage, delImage } from '@/api/image'
export default {
  name: 'ImageIndex',
  components: {},
  props: {},
  data () {
    const user = JSON.parse(window.localStorage.getItem('user'))
    return {
      collect: false,
      images: [],
      loading: true,
      totalCount: 0,
      pageSize: 16,
      page: 1,
      dialogVisible: false,
      uploadHeaders: {
        Authorization: 'Bearer ' + user.token
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadImages()
  },
  mounted () {},
  methods: {
    loadImages (page = 1) {
      this.loading = true
      getImages({
        per_page: this.pageSize,
        page,
        collect: this.collect
      }).then(res => {
        this.images = res.data.data.results
        this.loading = false
        this.totalCount = res.data.data.total_count
      })
    },
    onCollectChange (value) {
      this.collect = value
      this.loadImages()
    },
    onCurrentChange (page) {
      this.loadImages(page)
      // console.log(page)
    },
    onSetImg (articleId, img) {
      setImage(articleId, !img.is_collected).then(res => {
      })
      img.is_collected = !img.is_collected
    },
    ondelImg (articleId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delImage(articleId).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.loadImages(this.page)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      })
    },
    handleClose () {
      console.log('handleClose')
    },
    onUploadSuccess () {
      this.dialogVisible = false
      this.collect = false
      this.loadImages(this.page)
    }
  }
}
</script>

<style lang="less" scoped>
  .Img {
    position: relative;
  }
  .del {
    display: flex;
    align-items: center;
    position: absolute;
    width: 100%;
    height: 40px;
    background: rgba(000, 000, 000, .5);
    bottom: 2%;
    i {
      text-align: center;
      font-size: 25px;
      width: 50%;
      color:white;
      opacity: .9;
    }
    .ico1 {
      color: red;
    }
  }
  .action-head {
    padding-bottom: 20px;
    display: flex;
    justify-content: space-between;
  }
</style>
